//
//  ASBaseNavigationController.m
//
//  Created by wFeng on 14-8-12.
//  Copyright (c) 2014年. All rights reserved.
//

#import "ASBaseNavigationController.h"
//#import "RCSBaseViewController.h"

@interface ASBaseNavigationController()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@property(nonatomic, assign)  BOOL isViewShow;
@end

@implementation ASBaseNavigationController

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        [[ NSNotificationCenter defaultCenter ] addObserver:self
                                                   selector:@selector(layoutControllerSubViews)
                                                       name:UIApplicationDidChangeStatusBarFrameNotification
                                                     object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.delegate = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
        
        self.interactivePopGestureRecognizer.delaysTouchesBegan = NO;
    }

    [self setCustomSkin];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//设置自定义皮肤
-(void)setCustomSkin
{
    if (SYSTEM_VERSION >= 7.0)
    {
		UIImage *syncBgImg = [UIImage imageNamed:@"rectangle"];
        [self.navigationBar setBackgroundImage:syncBgImg
                                forBarPosition:UIBarPositionAny
                                    barMetrics:UIBarMetricsDefault];
//        [self.navigationBar setBackgroundColor:RGB(237, 238, 239)];
        
//        _barImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 8, 200, 27)];
//        [_barImageView setImage:[UIImage imageNamed:@"logo.png"]];
//        [_barImageView setBackgroundColor:[UIColor clearColor]];
//        [self.navigationBar addSubview:_barImageView];
    }
    else
    {
        UIImage *syncBgImg =  [UIImage imageNamed:@"rectangle"];
        [self.navigationBar setBackgroundImage:syncBgImg forBarMetrics:UIBarMetricsDefault];
        /*lch add 2014-10-17 [不加下面这句话，在ios7以下的版本，navigationbar显示不对] */
        self.navigationBar.clipsToBounds = YES;
        /*end*/
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    //return UIStatusBarStyleLightContent;
    return UIStatusBarStyleDefault;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.isViewShow)
    {
        return;
    }
    if(animated)
    {
        self.isViewShow = YES;
    }
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}


-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (self.viewControllers.count > 1)
    {
//        RCSBaseViewController *viewC = [self.viewControllers lastObject];
//        if (viewC && [viewC respondsToSelector:@selector(canDragBack)]) {
//            return [viewC canDragBack];
//        }
        return YES;
    }
    else
    {
        return NO;
    }
}
//
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
//{
//    return NO;
//}
//
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
//{
//    return YES;
//}
//
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
//{
//    return YES;
//}
//
//// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
//{
//    return NO;
//}

//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController*)navigationController
       didShowViewController:(UIViewController*)viewController
                    animated:(BOOL)animate
{

    self.isViewShow = NO;
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = YES;
}

/**
 *	@brief	通过ViewController的class在navi的ViewControllers查找类对象
 *
 *	@param 	aClass
 *
 *	@return	对象名，没有查找到返回nil
 */
-(UIViewController*)getViewControllerByClass:(Class)aClass
{
    NSArray* controllerArray = [self viewControllers];
    for (UIViewController* controlView in controllerArray)
    {
        if ([controlView isKindOfClass:aClass])
        {
            return controlView;
        }
    }
    
    return nil;
}

- (UIViewController*)getLastViewController
{
    NSArray* controllerArray = [self viewControllers];
    if (controllerArray.count > 0)
    {
        return [controllerArray lastObject];
    }
    return nil;
}

-(void)hiddBarImage
{
    self.barImageView.hidden = YES;
}

-(void)showBarImage
{
    self.barImageView.hidden = NO;
}

-(void)layoutControllerSubViews
{

}

#ifdef RCSCustomTabBar
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController.hidesBottomBarWhenPushed)
    {
        [RCS_APP_DELEGATE.tabbarViewController setTabBarHidden:YES animated:YES];
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (!viewController.hidesBottomBarWhenPushed)
    {
        [RCS_APP_DELEGATE.tabbarViewController setTabBarHidden:NO animated:YES];
    }
}
#endif

@end
